﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ConquerServer_Basic
{
    public class NPC : IClassPacket, INpc, IMapObject
    {
        private byte[] Packet;
        private ushort m_MapID;

        public NPC()
        {
            Packet = new byte[20];
            PacketBuilder.WriteUInt16(20, Packet, 0);
            PacketBuilder.WriteUInt16(2030, Packet, 2);
        }

        public uint UID
        {
            get { return BitConverter.ToUInt32(Packet, 4); }
            set { PacketBuilder.WriteUInt32(value, Packet, 4); }
        }

        public ushort X
        {
            get { return BitConverter.ToUInt16(Packet, 8); }
            set { PacketBuilder.WriteUInt16(value, Packet, 8); }
        }

        public ushort Y
        {
            get { return BitConverter.ToUInt16(Packet, 10); }
            set { PacketBuilder.WriteUInt16(value, Packet, 10); }
        }

        public ushort Type
        {
            get { return BitConverter.ToUInt16(Packet, 12); }
            set { PacketBuilder.WriteUInt16(value, Packet, 12); }
        }

        public ConquerAngle Facing
        {
            get { return (ConquerAngle)Packet[14]; }
            set { Packet[14] = (byte)value; }
        }

        public uint StatusFlag
        {
            get { return BitConverter.ToUInt32(Packet, 16); }
            set { PacketBuilder.WriteUInt32(value, Packet, 16); }
        }

        public ushort MapID
        {
            get { return m_MapID; }
            set { m_MapID = value; }
        }

        public MapObjectType MapObjType
        {
            get { return MapObjectType.Npc; }
        }

        public object Owner
        {
            get { return this; }
        }

        public void SendSpawn(GameClient Client, bool Ignore)
        {
            if (Client.Screen.Add(this) || Ignore)
            {
                Client.Send(Packet);
            }
        }

        public void SendSpawn(GameClient Client)
        {
            SendSpawn(Client, false);
        }

        public byte[] Serialize()
        {
            return Packet;
        }

        public void Deserialize(byte[] Bytes)
        {
            Packet = Bytes;
        }

        public static void Load()
        {
            int count = 0;
            IniFile cq_npc = new IniFile();
            foreach (string file in Directory.GetFiles(Database.DatabasePath + "\\cq_npc\\"))
            {
                cq_npc.FileName = file;
                INpc npc = new NpcSpawnPacket();
                npc.UID = cq_npc.ReadUInt32("cq_npc", "id", 0);
                npc.X = cq_npc.ReadUInt16("cq_npc", "cellx", 0);
                npc.Y = cq_npc.ReadUInt16("cq_npc", "celly", 0);
                npc.MapID = cq_npc.ReadUInt16("cq_npc", "mapid", 0);
                npc.Type = cq_npc.ReadUInt16("cq_npc", "lookface", 0);
                npc.Facing = (ConquerAngle)cq_npc.ReadUInt16("cq_npc", "type", 0);
                npc.StatusFlag = cq_npc.ReadUInt16("cq_npc", "sort", 0);
                count++;

            GetNpcDictionary:
                Dictionary<uint, INpc> npcs;
                if (Kernel.Npcs.TryGetValue(npc.MapID, out npcs))
                {
                    npcs.Add(npc.UID, npc);
                }
                else
                {
                    npcs = new Dictionary<uint, INpc>();
                    Kernel.Npcs.Add(npc.MapID, npcs);
                    goto GetNpcDictionary;
                }
            }

            Console.WriteLine("Npcs Loaded: "+ count);
        }
    }
}
